package org.jeecg.modules.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.travel.code.CheckStatusCode;
import org.jeecg.modules.travel.dto.slideshow.SlideshowAddDto;
import org.jeecg.modules.travel.dto.slideshow.SlideshowAuthDto;
import org.jeecg.modules.travel.dto.slideshow.SlideshowEditDto;
import org.jeecg.modules.travel.entity.JhSlideshow;
import org.jeecg.modules.travel.mapper.JhSlideshowMapper;
import org.jeecg.modules.travel.service.IJhSlideshowService;
import org.jeecg.modules.travel.vo.slideshow.AppSlideshowRecordVo;
import org.jeecg.modules.travel.vo.slideshow.PcSlideshowRecordVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class JhSlideshowServiceImpl extends ServiceImpl<JhSlideshowMapper,JhSlideshow> implements IJhSlideshowService {

    @Override
    public IPage<PcSlideshowRecordVo> queryPage(Page<JhSlideshow> page, QueryWrapper<JhSlideshow> queryWrapper) {
        return baseMapper.queryPage(page,queryWrapper);
    }

    @Override
    public String slideshowAdd(SlideshowAddDto dto) {
        JhSlideshow slideshow = new JhSlideshow();
        BeanUtils.copyProperties(dto, slideshow);
        boolean flag = save(slideshow);
        if (!flag) {
            return "添加失败";
        }
        return null;
    }

    @Override
    public String slideshowEdit(SlideshowEditDto dto) {
        JhSlideshow slideshow = getById(dto.getId());
        if (slideshow == null) {
            return "幻灯片记录不存在";
        }
        UpdateWrapper<JhSlideshow> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(JhSlideshow::getId,dto.getId())
                .set(JhSlideshow::getSlideshowName,dto.getSlideshowName())
                .set(JhSlideshow::getSlideshowUrl,dto.getSlideshowUrl())
                .set(JhSlideshow::getSortNo,dto.getSortNo());
        boolean flag = update(updateWrapper);
        if (!flag) {
            return "编辑失败";
        }
        return null;
    }

    @Override
    public String slideshowAuth(SlideshowAuthDto dto, String userId) {
        JhSlideshow slideshow = getById(dto.getId());
        if (slideshow == null) {
            return "幻灯片记录不存在";
        }
        UpdateWrapper<JhSlideshow> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(JhSlideshow::getCheckStatus, dto.getCheckStatus())
                .set(JhSlideshow::getCheckPeople, userId)
                .set(JhSlideshow::getCheckTime, new Date())
                .set(JhSlideshow::getCheckRemark, Objects.equals(dto.getCheckStatus(), CheckStatusCode.NOT_APPROVED)
                        ? dto.getCheckRemark() : null)
                .eq(JhSlideshow::getId, dto.getId());
        boolean flag = update(wrapper);
        if (!flag) {
            return "审核失败";
        }
        return null;
    }

    @Override
    public IPage<AppSlideshowRecordVo> appQueryList(Page<JhSlideshow> page, QueryWrapper<JhSlideshow> queryWrapper) {
        return baseMapper.bappQueryList(page,queryWrapper);
    }

}
